/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


//
function r_regist() {

    h = $('[name=r_nickname]').val();
    i = $('[name=r_pwd]').val();
    j = $('[name=r_repwd]').val();

    if (h === "") {
        alert('登陆名不能为空');
        return;
    }
    if (i === "" || j === "") {
        alert('密码不能为空');
        return;
    }

    if (i != j) {
        alert('两次密码不一致!');
        return;
    }

    $.ajax({
        data: {
            name: h,
            pwd: i,
        },
        type: 'post',
        url: "/index.php/User/User/doRegist",
        dataType: "json",
        success: function (data) {
            document.location = data.h;
        },
        error: function (data) {
            alert('注册失败,用户已存在!');
        }
    })
}

//修改
function md_modify() {
    if ($('[name=md_pwd]').val() == '') {
        alert('请输入旧密码!');
        return;
    }

    if ($('[name=md_newpwd]').val() == '') {
        alert('请输入新密码');
        return;
    }

    if ($('[name=md_repwd]').val() == '') {
        alert('请重复新密码');
        return;
    }

    if ($('[name=md_newpwd]').val() != $('[name=md_repwd]').val()) {
        alert('两次密码不一致');
        return;
    }

    $.ajax({
        data: {
            oldPassword: $('[name=md_pwd]').val(),
            password: $('[name=md_newpwd]').val()
        },
        type: 'post',
        dataType: "json",
        url: "/index.php/User/User/doModify",
        //dataType: "json",
        success: function (data) {
            document.location = data.h;
        },
        error: function (data) {
            alert('修改密码失败,请检查旧密码是否正确!');
        }
    });
}

//登陆页面函数
function lg_login() {
    name = $('[name=lg_nickname]').val();

    if (name == "") {
        alert('用户名不能为空');
        return;
    }

    pwd = $('[name=lg_password]').val()

    if (pwd == "") {
        alert('密码不能为空');
        return;
    }

    time = new Date();
    time = time.getMilliseconds();

    //jquery 有 bug 不能获取到 checkbox 的选中状态
    remember = document.getElementById("lg_rememberMe").checked;

    $.ajax({
        data: {
            username: name,
            password: pwd,
            time: time,
            remember: remember
        },
        type: 'post',
        url: "/index.php/User/User/LoginCheck",
        dataType: "json",
        success: function (data) {
            document.location = data.h;
        },
        error: function (data) {
            alert('登陆失败,请检查用户名和密码!');
        }
    });
}

//任务提交页面函数
function ir_require() {
    title = $('[name=ir_title]').val();
    content = $('[name=ir_content]').val();
    reward = $('[name=ir_reward]').val();

    if (title == "") {
        alert('标题不能为空!');
        return;
    }
    if (content == "")
    {
        alert('描述不能为空!');
        return;
    }
    if (reward == "") {
        alert('赏金不能为空!');
        return;
    }

    regex = /[^0-9^\.]/;

    if (regex.exec(reward) != null) {
        alert('赏金不能含有非数字字符');
        return;
    }

    $.ajax({
        data: {
            title: title,
            content: content,
            reward: reward
        },
        dataType: 'json',
        type: 'get',
        url: "/index.php/User/User/doRequire",
        success: function (data) {
            document.location = data.h;
        },
        error: function (data) {
            alert('发布失败,请检查信息,确认后从新发布!');
        }
    });
}


//搜索页面函数
function if_Search() {
    if ($('#if_quitSearch').val() == '') {
        return;
    }

    $.ajax({
        dataType: "json",
        type: 'GET',
        url: "/index.php/User/User/doFind",
        data: {
            keyword: $('#if_quitSearch').val()
        },
        success: function (data) {
            $('#if_title').html(data.title);
            $('#if_content').html(data.content);
            //$('#if_user').html(data.user);
        },
    });
}

//搜索函数
function h_search() {
    if ($('#h_keyword').val() == '') {
        document.location = "/index.php/User/User/issueFind";
        return;
    }
}

//接受任务函数
function ti_doRecv(){
    id = $('[name=ir_TaskID]').val();
    $.ajax({
        data: {
            ID: id
        },
        type: 'get',
        url: "/index.php/User/User/doRecv",
        success: function () {
            location.reload();
        }
    });
}